package com.tomecode.mjprocessor.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

/**
 * Parse maven model and etc
 * 
 * @author Frastia Tomas
 */
public final class MavenResolver
{

    /**
     * find pom.xml in root and parse
     * 
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     * @throws XmlPullParserException
     */
    public static final Model getModel( File dir )
        throws FileNotFoundException, IOException, XmlPullParserException
    {
        if ( dir.toString().equals( "..\\pom.xml" ) )
        {
            return new MavenXpp3Reader().read( new FileReader( dir.getAbsoluteFile() ) );
        }

        // if is directory
        if ( dir.isDirectory() )
        {
            File[] files = dir.listFiles();
            for ( File file : files )
            {
                if ( file.isFile() && file.getName().equals( "pom.xml" ) )
                {
                    return new MavenXpp3Reader().read( new FileReader( file ) );
                }
            }
        }
        return new MavenXpp3Reader().read( new FileReader( dir ) );
    }

    /**
     * load all maven properties in project
     * 
     * @param model - current loaded maven model
     * @return
     * @throws XmlPullParserException
     * @throws IOException
     * @throws FileNotFoundException
     */
    public static final Properties loadProperties( Model model, File baseDir )
        throws FileNotFoundException, IOException, XmlPullParserException
    {
        if ( baseDir.toString().equals( "..\\pom.xml" ) )
        {
            baseDir = baseDir.getAbsoluteFile();
        }

        return loadPropertiesFromPoms( model, baseDir );
    }

    /**
     * load all properties in poms
     * 
     * @param model
     * @param baseDir
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     * @throws XmlPullParserException
     */
    private static final Properties loadPropertiesFromPoms( Model model, File baseDir )
        throws FileNotFoundException, IOException, XmlPullParserException
    {
        Properties properties = new Properties();
        properties.putAll( model.getProperties() );
        while ( model.getParent() != null )
        {
            Parent parent = model.getParent();
            if ( baseDir.isFile() )
            {
                baseDir = new File( baseDir.getParent() + File.separator + baseDir );
            }
            File parentFile = new File( baseDir + File.separator + parent.getRelativePath() );
            model = getModel( parentFile );
            properties.putAll( model.getProperties() );
            baseDir = parentFile.getParentFile();
        }

        return properties;
    }

}
